﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using ThridGroup.ERP.DTO.ProductionSystem.ShiftSchedulingManagement.Procurement;
using ThridGroup.ERP.DTO.WarehouseManagement;
using ThridGroup.ERP.WarehouseManagement;

namespace ThridGroup.ERP.Web.Controllers.ProductionSystem
{
    /// <summary>
    /// 仓储管理 控制器
    /// </summary>
    [Route("[controller]")]
    public class WarehouseManagementController : Controller
    {
        /// <summary>
        /// 采购入库服务接口
        /// </summary>
        private readonly IProcurementServices _procurementServices;

        /// <summary>
        /// 装箱服务接口
        /// </summary>
        private readonly IPackingServices _packingServices;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="procurementServices">采购入库服务</param>
        /// <param name="packingServices">装箱服务</param>
        public WarehouseManagementController(IProcurementServices procurementServices, IPackingServices packingServices)
        {
            _procurementServices = procurementServices;
            _packingServices = packingServices;
        }

        

        /// <summary>
        /// 采购入库管理视图
        /// </summary>
        /// <returns></returns>
        [Route("ProcurementView")]
        public IActionResult ProcurementView()
        {
            return View();
        }

        /// <summary>
        /// 添加采购入库视图
        /// </summary>
        /// <returns></returns>
        [Route("CreateProcurementView")]
        public IActionResult CreateProcurementView()
        {
            return View();
        }

        /// <summary>
        /// 修改采购入库视图
        /// </summary>
        /// <returns></returns>
        [Route("UpdateProcurementView")]
        public IActionResult UpdateProcurementView()
        {
            return View();
        }

        /// <summary>
        /// 添加采购入库
        /// </summary>
        /// <param name="dto">采购入库信息</param>
        /// <returns></returns>
        [HttpPost]
        [Route("CreateProcurement")]
        public async Task<IActionResult> CreateProcurement([FromBody] ProcurementDto dto)
        {
            try
            {
                var result = await _procurementServices.CreateProcurement(dto);
                return Json(new { success = result > 0, message = result > 0 ? "添加成功" : "添加失败" });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "添加失败：" + ex.Message });
            }
        }

        /// <summary>
        /// 查询采购入库（分页）
        /// </summary>
        /// <param name="dto">查询参数</param>
        /// <returns></returns>
        [HttpPost]
        [Route("QueryProcurement")]
        public async Task<IActionResult> QueryProcurement([FromBody] GetProcurementDto dto)
        {
            try
            {
                var result = await _procurementServices.QueryProcurement(dto);
                return Json(new { code = 0, msg = "查询成功", count = result.TotalCount, data = result.Data });
            }
            catch (Exception ex)
            {
                return Json(new { code = 1, msg = "查询失败：" + ex.Message, count = 0, data = new List<ProcurementDto>() });
            }
        }

        /// <summary>
        /// 修改采购入库
        /// </summary>
        /// <param name="dto">采购入库信息</param>
        /// <returns></returns>
        [HttpPost]
        [Route("UpdateProcurement")]
        public async Task<IActionResult> UpdateProcurement([FromBody] ProcurementDto dto)
        {
            try
            {
                var result = await _procurementServices.UpdateProcurement(dto);
                return Json(new { success = result > 0, message = result > 0 ? "修改成功" : "修改失败" });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "修改失败：" + ex.Message });
            }
        }

        /// <summary>
        /// 根据ID获取采购入库信息
        /// </summary>
        /// <param name="id">采购入库ID</param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetProcurementById")]
        public async Task<IActionResult> GetProcurementById(int id)
        {
            try
            {
                var result = await _procurementServices.GetProcurementById(id);
                return Json(new { success = true, data = result });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "获取失败：" + ex.Message });
            }
        }

        /// <summary>
        /// 批量删除采购入库
        /// </summary>
        /// <param name="ids">采购入库ID列表</param>
        /// <returns></returns>
        [HttpPost]
        [Route("BatchDeleteProcurement")]
        public async Task<IActionResult> BatchDeleteProcurement([FromBody] List<int> ids)
        {
            try
            {
                await _procurementServices.BatchDeleteProcurement(ids);
                return Json(new { success = true, message = "删除成功" });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "删除失败：" + ex.Message });
            }
        }
        /// <summary>
        /// 根据ID获取采购入库信息修改入库状态
        /// </summary>
        /// <param name="id">采购入库ID</param>
        /// <returns></returns>
        [HttpPut]
        [Route("GetProcurementStatus")]
        public async Task<IActionResult> GetProcurementStatus(int id)
        {
            try
            {
                await _procurementServices.GetProcurementStatus(id);
                return Json(new { success = true, message = "修改成功" });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "修改失败：" + ex.Message });
            }
        }

        #region 装箱管理

        /// <summary>
        /// 装箱管理视图
        /// </summary>
        /// <returns></returns>
        [Route("PackingView")]
        public IActionResult PackingView()
        {
            return View();
        }

        /// <summary>
        /// 添加装箱视图
        /// </summary>
        /// <returns></returns>
        [Route("CreatePackingView")]
        public IActionResult CreatePackingView()
        {
            return View();
        }

        /// <summary>
        /// 修改装箱视图
        /// </summary>
        /// <returns></returns>
        [Route("UpdatePackingView")]
        public IActionResult UpdatePackingView()
        {
            return View();
        }

        /// <summary>
        /// 添加装箱
        /// </summary>
        /// <param name="dto">装箱信息</param>
        /// <returns></returns>
        [HttpPost]
        [Route("CreatePacking")]
        public async Task<IActionResult> CreatePacking([FromBody] QueryPackingDto dto)
        {
            try
            {
                var result = await _packingServices.CreatePacking(dto);
                return Json(new { success = result > 0, message = result > 0 ? "添加成功" : "添加失败" });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "添加失败：" + ex.Message });
            }
        }

        /// <summary>
        /// 查询装箱（分页）
        /// </summary>
        /// <param name="dto">查询参数</param>
        /// <returns></returns>
        [HttpPost]
        [Route("QueryPacking")]
        public async Task<IActionResult> QueryPacking([FromBody] GetPackingDto dto)
        {
            try
            {
                var result = await _packingServices.QueryPacking(dto);
                return Json(new { code = 0, msg = "查询成功", count = result.TotalCount, data = result.Data });
            }
            catch (Exception ex)
            {
                return Json(new { code = 1, msg = "查询失败：" + ex.Message, count = 0, data = new List<QueryPackingDto>() });
            }
        }

        /// <summary>
        /// 修改装箱
        /// </summary>
        /// <param name="dto">装箱信息</param>
        /// <returns></returns>
        [HttpPost]
        [Route("UpdatePacking")]
        public async Task<IActionResult> UpdatePacking([FromBody] QueryPackingDto dto)
        {
            try
            {
                var result = await _packingServices.UpdatePacking(dto);
                return Json(new { success = result > 0, message = result > 0 ? "修改成功" : "修改失败" });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "修改失败：" + ex.Message });
            }
        }

        /// <summary>
        /// 根据ID获取装箱信息
        /// </summary>
        /// <param name="id">装箱ID</param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetPackingById")]
        public async Task<IActionResult> GetPackingById(int id)
        {
            try
            {
                var result = await _packingServices.GetPackingById(id);
                return Json(new { success = true, data = result });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "获取失败：" + ex.Message });
            }
        }

        /// <summary>
        /// 批量删除装箱
        /// </summary>
        /// <param name="ids">装箱ID列表</param>
        /// <returns></returns>
        [HttpPost]
        [Route("BatchDeletePacking")]
        public async Task<IActionResult> BatchDeletePacking([FromBody] List<int> ids)
        {
            try
            {
                await _packingServices.BatchDeletePacking(ids);
                return Json(new { success = true, message = "删除成功" });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "删除失败：" + ex.Message });
            }
        }

        /// <summary>
        /// 根据ID获取装箱信息修改状态
        /// </summary>
        /// <param name="id">装箱ID</param>
        /// <returns></returns>
        [HttpPut]
        [Route("GetPackingStatus")]
        public async Task<IActionResult> GetPackingStatus(int id)
        {
            try
            {
                await _packingServices.GetPackingStatus(id);
                return Json(new { success = true, message = "修改成功" });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "修改失败：" + ex.Message });
            }
        }


      


        /// <summary>
        /// 分页查询装箱明细
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("QueryPackingDetailsModel")]
        public async Task<IActionResult> QueryPackingDetailsModel(string PackingCode, int page = 1, int limit = 3)
        {
            try
            {
                var result = await _packingServices.QueryPackingDetailsModel(PackingCode,page, limit);
                return Json(new { code = 0, msg = "查询成功", count = result.Count, data = result });
            }
            catch (Exception ex)
            {
                return Json(new { code = 1, msg = "查询失败：" + ex.Message, count = 0, data = new List<PackingDetailsModelDto>() });
            }
        }

        /// <summary>
        /// 修改装箱明细
        /// </summary>
        /// <param name="dto">装箱明细信息</param>
        /// <returns></returns>
        [HttpPost]
        [Route("UpdatePackingDetailsModel")]
        public async Task<IActionResult> UpdatePackingDetailsModel([FromBody] PackingDetailsModelDto dto)
        {
            try
            {
                var result = await _packingServices.UpdatePackingDetailsModel(dto);
                return Json(new { success = result > 0, message = result > 0 ? "修改成功" : "修改失败" });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "修改失败：" + ex.Message });
            }
        }

        /// <summary>
        /// 批量删除装箱明细
        /// </summary>
        /// <param name="ids">装箱明细ID列表</param>
        /// <returns></returns>
        [HttpPost]
        [Route("BatchDeletePackingDetailsModel")]
        public async Task<IActionResult> BatchDeletePackingDetailsModel([FromBody] List<int> ids)
        {
            try
            {
                var result = await _packingServices.BatchDeletePackingDetailsModel(ids);
                return Json(new { success = result > 0, message = result > 0 ? "删除成功" : "删除失败" });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "删除失败：" + ex.Message });
            }
        }

        /// <summary>
        /// 添加装箱明细
        /// </summary>
        /// <param name="dto">装箱明细信息</param>
        /// <returns></returns>
        [HttpPost]
        [Route("AddPackingDetailsModelS")]
        public async Task<IActionResult> AddPackingDetailsModelS([FromBody] PackingDetailsModelDto dto)
        {
            try
            {
                var result = await _packingServices.AddPackingDetailsModelS(dto);
                return Json(new { success = result > 0, message = result > 0 ? "添加成功" : "添加失败" });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "添加失败：" + ex.Message });
            }
        }

        #endregion

        // ================== 物料入库 ==================

        /// <summary>
        /// 添加物料入库
        /// </summary>
        [HttpPost]
        [Route("CreateMaterialReceiptLine")]
        public async Task<IActionResult> CreateMaterialReceiptLine([FromBody] MaterialReceiptLineDto dto)
        {
            try
            {
                var result = await _procurementServices.CreateMaterialReceiptLine(dto);
                return Json(new { success = result > 0, message = result > 0 ? "添加成功" : "添加失败" });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "添加失败：" + ex.Message });
            }
        }

        /// <summary>
        /// 查询物料入库（分页/条件）
        /// </summary>
        [HttpPost]
        [Route("QueryMaterialReceiptLine")]
        public async Task<IActionResult> QueryMaterialReceiptLine([FromBody] MaterialReceiptLineQueryDto dto)
        {
            try
            {
                var result = await _procurementServices.QueryMaterialReceiptLine(dto.LnboundCode, dto.page, dto.limit);
                return Json(new { code = 0, msg = "查询成功", count = result.TotalCount, data = result.Data });
            }
            catch (Exception ex)
            {
                return Json(new { code = 1, msg = "查询失败：" + ex.Message, count = 0, data = new List<MaterialReceiptLineDto>() });
            }
        }

        /// <summary>
        /// 修改物料入库
        /// </summary>
        [HttpPost]
        [Route("UpdateMaterialReceiptLine")]
        public async Task<IActionResult> UpdateMaterialReceiptLine([FromBody] MaterialReceiptLineDto dto)
        {
            try
            {
                var result = await _procurementServices.UpdateMaterialReceiptLine(dto);
                return Json(new { success = result > 0, message = result > 0 ? "修改成功" : "修改失败" });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "修改失败：" + ex.Message });
            }
        }

        /// <summary>
        /// 根据ID获取物料入库信息
        /// </summary>
        [HttpGet]
        [Route("GetMaterialReceiptLineById")]
        public async Task<IActionResult> GetMaterialReceiptLineById(int id)
        {
            try
            {
                var result = await _procurementServices.GetMaterialReceiptLineById(id);
                return Json(new { success = true, data = result });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "获取失败：" + ex.Message });
            }
        }

        /// <summary>
        /// 批量删除物料入库
        /// </summary>
        [HttpPost]
        [Route("BatchDeleteMaterialReceiptLine")]
        public async Task<IActionResult> BatchDeleteMaterialReceiptLine([FromBody] List<int> ids)
        {
            try
            {
                await _procurementServices.BatchDeleteMaterialReceiptLine(ids);
                return Json(new { success = true, message = "删除成功" });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "删除失败：" + ex.Message });
            }
        }
    }
}
